package Day2025_04_02;

import java.util.ArrayList;
import java.util.List;

public class LeetCode46 {
    List<List<Integer>> list;
    List<Integer> ret;
    boolean[] tmp;
    public List<List<Integer>> permute(int[] nums) {
        list = new ArrayList<>();
        ret = new ArrayList<>();
        tmp = new boolean[nums.length];
        dfs (nums);
        return list;
    }
    private void dfs (int[] nums) {
        if (ret.size() == nums.length) {
            list.add(new ArrayList<>(ret));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (tmp[i] == false) {
                ret.add(nums[i]);
                tmp[i] = true;
                dfs (nums);
                tmp[i] = false;
                ret.remove(ret.size() - 1);
            }
        }
    }
}
